package com.coursehub.exercise.controller;

import com.coursehub.exercise.entity.WrongQuestionBank;
import com.coursehub.exercise.service.WrongQuestionBankService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/exercises/wrong-banks")
@RequiredArgsConstructor
public class WrongQuestionBankController {

    private final WrongQuestionBankService wrongQuestionBankService;

    @PostMapping
    public ResponseEntity<WrongQuestionBank> create(@RequestBody Map<String, String> body) {
        WrongQuestionBank bank = wrongQuestionBankService.create(
                body.get("wrongQuestionBankId"),
                body.get("userId"),
                body.get("courseId"));
        return ResponseEntity.ok(bank);
    }

    @DeleteMapping("/{wrongQuestionBankId}")
    public ResponseEntity<Void> delete(@PathVariable String wrongQuestionBankId) {
        wrongQuestionBankService.delete(wrongQuestionBankId);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<WrongQuestionBank>> listByUser(@PathVariable String userId) {
        return ResponseEntity.ok(wrongQuestionBankService.listByUser(userId));
    }

    @GetMapping("/user/{userId}/course/{courseId}")
    public ResponseEntity<List<WrongQuestionBank>> listByUserAndCourse(@PathVariable String userId, @PathVariable String courseId) {
        return ResponseEntity.ok(wrongQuestionBankService.listByUserAndCourse(userId, courseId));
    }

    @PostMapping("/{wrongQuestionBankId}/questions/{questionId}")
    public ResponseEntity<Void> addQuestion(@PathVariable String wrongQuestionBankId, @PathVariable String questionId) {
        wrongQuestionBankService.addQuestion(wrongQuestionBankId, questionId);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{wrongQuestionBankId}/questions/{questionId}")
    public ResponseEntity<Void> removeQuestion(@PathVariable String wrongQuestionBankId, @PathVariable String questionId) {
        wrongQuestionBankService.removeQuestion(wrongQuestionBankId, questionId);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/{wrongQuestionBankId}/questions")
    public ResponseEntity<List<String>> listQuestions(@PathVariable String wrongQuestionBankId) {
        return ResponseEntity.ok(wrongQuestionBankService.listQuestionIds(wrongQuestionBankId));
    }
}


